#ifndef __APP_MWIFI_H__
#define __APP_MWIFI_H__

#include "mlink.h"
#include "mwifi.h"
#include "esp_system.h" 

#include "wifi_nvs.h"
#include "wifi_mupgrade.h"

 
#define CONFIG_MWIFI_ID             {0x19, 0x96, 0x08, 0x27, 0x53, 0x70}  /**< uint8_t mesh_id[6];  Mesh network ID. Nodes sharing the same MESH ID can communicate with one another */
#define CONFIG_MWIFI_GROUP_ID       {0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA}  /**< wifi mesh group id addr */
#define CONFIG_MWIFI_PASSWROD       "yiree2022"         /**< char mesh_password[64];    Password for secure communication between devices in a MESH network */
#define CONFIG_MWIFI_CHANNEL         11                 /**< uint8_t channel;           Channel, mesh and router will be on the same channel */
#define CONFIG_MWIFI_MESH_TYPE       MWIFI_MESH_ROOT    /**< mwifi_node_type_t mesh_type;  Only MWIFI_MESH_IDLE, MWIFI_MESH_ROOT, MWIFI_MESH_NODE and MWIFI_MESH_LEAF device types are supported. */
#define CONFIG_ESPNOW_KEY            "lmk-yiree2022"    /**< Espnow communication encrypted key */
#define CONFIG_ESPNOW_MKEY           "pmk-yiree2022"    /**< primary master key is used to encrypt local master key */
 
#define MWIFI_WRITE_DATA_LEN          256                // MW1IFI_PAYLOAD_LEN
#define MWIFI_READ_DATA_LEN           1032               // MW1IFI_PAYLOAD_LEN

#define NETWORK_SELECTION_WIFI      0X00
#define NETWORK_SELECTION_ETH       0X02
#define NETWORK_SELECTION_4G        0X04

#ifndef ADDR2STR
#define ADDR2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
#define ADDRSTR "%02x%02x%02x%02x%02x%02x"
#endif

/*!< data_type.custom */
#define MWIFI_TYPE_NODE     0x00
#define MWIFI_TYPE_ROOT     0x01
#define MWIFI_TYPE_GROUP    0x02
#define MWIFI_TYPE_TOPO     0x04
#define MWIFI_TYPE_MOTOR    0x10   // 发给电机
#define MWIFI_TYPE_APP      0x81

typedef struct {
    uint8_t addr[6];
    mwifi_data_type_t data_type;
    uint8_t data[MWIFI_WRITE_DATA_LEN];
    size_t  size;
} mwifi_write_t;  /* 节点发送数据结构体 */
 

// ret = mwifi_read(src_addr, &data_type, data, &size, portMAX_DELAY);
typedef struct {
    uint8_t addr[6];
    mwifi_data_type_t data_type;
    uint8_t *data;
    size_t   size;
} mwifi_read_t;  /* 节点接收数据结构体 */

typedef void (*mwifi_func_t)(mwifi_read_t);

void mwifi_register_callback(mwifi_func_t callback_func);

// void mwifi_register_ota_callback(mwifi_func_t callback_func);

int app_mwifi_root_upgrade_read(ota_result_t *packet, size_t wait_time); 

void app_mwifi_root_upgrade_clear(void);
 
// root节点 发送给其它 node节点
int app_mwifi_root_write(const uint8_t addr[6], uint8_t type, const void *data, uint16_t len); 

int app_mwifi_root_upgrade_write(const uint8_t addr[6], const void *data, uint16_t len); 

void app_mwifi_init(nvs_wifi_t nvs_wifi);

void app_mwifi_net_addr(uint8_t addr[6]);
 
int app_mwifi_get_routing_table(uint8_t node_addr[6], uint8_t node_no);

char *app_mwifi_update_topo(void);

bool app_mwifi_routing_table_update(void);
 
bool node_get_upgrade_status(void);

bool mwifi_connect_status(uint32_t wait_time);

bool app_mwifi_reconnect(const char router_ssid[32], const char router_password[32]);

int8_t app_wifi_rssi(void);

esp_ip4_addr_t wifi_local_ip_addr(void);

void wifi_clear_connect_status(void);

void app_mwifi_deinit(void); 

#define wifi_connect_status(time) mwifi_connect_status(time)
 
#endif  /*__APP_MWIFI_H__ END.*/

